var roleHBRU_I = require('role.HBRU_I');
var roleE10S58_H_I = require('role.E10S58_H_I');
var roleE2S59_HBRU_I = require('role.E2S59_HBRU_I');
var roleE2S59_HBRU_II = require('role.E2S59_HBRU_II');
var roleE8S57_HBR_I = require('role.E8S57_HBR_I');
var roleE8S57_R_I = require('role.E8S57_R_I');
var roleE8S57_HBR_II = require('role.E8S57_HBR_II');
var roleE8S57_fighter_AH = require('role.E8S57_fighter_AH');
var roleE9S57_HBR_I = require('role.E9S57_HBR_I');
var roleE9S57_HBR_II = require('role.E9S57_HBR_II');
var roleE9S57_R_I = require('role.E9S57_R_I');
var roleE9S57_fighter_AH = require('role.E9S57_fighter_AH');
var roleE9S59_HC = require('role.E9S59_HC'); 
var roleE9S59_T_I = require('role.E9S59_T_I'); 
var roleE9S59_U = require('role.E9S59_U'); 
var roleHar = require('role.har'); 
var roleHar_o = require('role.har_o'); 
var roleStander = require('role.stander');
var roleStander_E8S57 = require('role.stander_E8S57');
var roleFighter = require('role.fighter');
var roleFighter_AH = require('role.fighter_AH');
var roleQiqiang = require('role.qiqiang');
var roleMushi = require('role.mushi');
var roleCarrier = require('role.carrier');
var roleCarrierdrop = require('role.carrierdrop');//兼职link
var roleUpgrader = require('role.upgrader');
var roleRepairer = require('role.repairer');
var roleBuilder = require('role.builder');
var roleBuilderout = require('role.builderout');
var roleTransporter = require('role.transporter');
var roleTransportertotower = require('role.transportertotower');
var roleHario = require('role.hario');
var roleCarrierio = require('role.carrierio');
var roleCarrierlab = require('role.carrierlab');
var labs = Game.rooms['E9S58'].find(FIND_MY_STRUCTURES,      {filter: {structureType: STRUCTURE_LAB}});

module.exports.loop = function () {
    console.log(Game.time)
    
    if(Game.time % 81 == 0 && labs[0].store.getUsedCapacity() != 0 && labs[1].store.getUsedCapacity() != 0){
        labs[2].runReaction(labs[0], labs[1]); 
    }
    
    //LINK大约11tick执行一次
    if(Game.time % 11 == 0){ 
        for(var name in Memory.creeps) {
            if(!Game.creeps[name]) {
                delete Memory.creeps[name];
                console.log('Clearing non-existing creep memory:', name);
            }
        }
    }
    
    if(Game.time % 19 == 0){ 
        const linkFrom = Game.rooms['E9S58'].lookForAt('structure',10, 16)[0];
        const linkFrom2 = Game.rooms['E9S58'].lookForAt('structure',35, 26)[0];
        const linkTo = Game.rooms['E9S58'].lookForAt('structure',29, 22)[0];
        console.log('LINK GO!');
        linkFrom.transferEnergy(linkTo);
        linkFrom2.transferEnergy(linkTo);
        if(Game.cpu.bucket == 10000) {
            Game.cpu.generatePixel();
        }
    }
    
    if(Game.time % 37 == 0){ 
        const linkFrom_E9S59 = Game.rooms['E9S59'].lookForAt('structure',38, 5)[0];
        const linkTo_E9S59 = Game.rooms['E9S59'].lookForAt('structure',6, 20)[0];
        if(linkTo_E9S59.store.getUsedCapacity(RESOURCE_ENERGY)<200){
            console.log('LINK_II GO!');
            linkFrom_E9S59.transferEnergy(linkTo_E9S59);
        }
    }
    
    if(Game.time % 53 == 0){ 
        //自动购买能量
        const targetRoom = "E9S58";
        var buyorder = Game.market.getAllOrders(order => order.resourceType == RESOURCE_ENERGY &&
            order.type == ORDER_SELL && order.price <1.000 &&
            Game.market.calcTransactionCost(1000, targetRoom, order.roomName) < 400);
            if(buyorder.length>0 && Game.getObjectById('621733def0d05e217ae4f74f').store.getUsedCapacity()<700000){
                console.log("这个可以买");
                console.log(buyorder[0].id);
                console.log(buyorder[0].amount);
                console.log(buyorder[0].roomName);
                Game.market.deal(buyorder[0].id, buyorder[0].amount, targetRoom);
            }
            
        //检测订单情况,剩余为0就加单
        const orderbuy = Game.market.getOrderById('62243977d769307b13d11dac');
        if(orderbuy.remainingAmount<10000 && Game.getObjectById('621733def0d05e217ae4f74f').store.getUsedCapacity()<500000){
            Game.market.extendOrder('62243977d769307b13d11dac', 50000);
            console.log("挂了1.3的50000买");
        }
            
    }
    
    if(Game.time % 59 == 0){ 
        //自动卖像素
        const targetRoom = "E9S58";
        var sellorder = Game.market.getAllOrders(order => order.resourceType == PIXEL &&
            order.type == ORDER_BUY && order.price >16000 );
            if(sellorder.length>0 && Game.resources.pixel > 20){
                console.log("这个可以卖");
                console.log(sellorder[0].id);
                console.log(sellorder[0].amount);
                console.log(sellorder[0].price);
                Game.market.deal(sellorder[0].id, 1, targetRoom);
            }
            
        const ordersell = Game.market.getOrderById('622b2f815a2b2c3601d9f711');
        if(ordersell.remainingAmount<=10000 && Game.getObjectById('621733def0d05e217ae4f74f').store.getUsedCapacity()>400000){
            Game.market.extendOrder('622b2f815a2b2c3601d9f711', 20000);
            console.log("挂了1.72的20000卖");
        }
        
        // const ordersell2 = Game.market.getOrderById('622c23bf5a2b2cbd1d286123');
        // if(ordersell2.remainingAmount<=10000 && Game.getObjectById('621733def0d05e217ae4f74f').store.getUsedCapacity()>400000){
        //     Game.market.extendOrder('622c23bf5a2b2cbd1d286123', 20000);
        //     console.log("挂了2.8的20000卖");
        // }
    }
    //E9S58
    var tower = Game.getObjectById('62199c4b15cd3edd278efbf8');
    if(tower) {
        var closestHostile1 = tower.room.find(FIND_HOSTILE_CREEPS);
        if(closestHostile1.length>0) {
            tower.attack(closestHostile1[0]);
        }
        else{
            var closestDamagedStructure1 = tower.pos.findClosestByRange(FIND_STRUCTURES, {
                filter: (structure) => (structure.hits < structure.hitsMax*0.5 && 
                    structure.structureType != STRUCTURE_WALL && structure.structureType != STRUCTURE_RAMPART|| 
                    structure.hits < 25000 && structure.structureType == STRUCTURE_RAMPART ||
                    structure.hits < 25000 && structure.structureType == STRUCTURE_WALL)});
            if(closestDamagedStructure1) {
                tower.repair(closestDamagedStructure1);
            }
        }
    }

    var tower2 = Game.getObjectById('621772c77fa0bc59655e0f4a');
    if(tower2) {
        var closestHostile2 = tower2.room.find(FIND_HOSTILE_CREEPS);
        if(closestHostile2.length>0) {
            tower2.attack(closestHostile2[closestHostile2.length-1]);
        }
        else{
            var closestDamagedStructure2 = tower2.pos.findClosestByRange(FIND_STRUCTURES, {
                filter: (structure) => (structure.hits < structure.hitsMax*0.5 && 
                    structure.structureType != STRUCTURE_WALL && structure.structureType != STRUCTURE_RAMPART|| 
                    structure.hits < 25000 && structure.structureType == STRUCTURE_RAMPART ||
                    structure.hits < 25000 && structure.structureType == STRUCTURE_WALL)});
            if(closestDamagedStructure2) {
                tower2.repair(closestDamagedStructure2);
            }
        }
    }
    
    var tower21 = Game.getObjectById('622b2411acd95e168085a6ef');
    if(tower21) {
        var closestHostile21 = tower21.room.find(FIND_HOSTILE_CREEPS);
        if(closestHostile21.length>0) {
            tower21.attack(closestHostile21[closestHostile21.length-1]);
        }
        else{
            var closestDamagedStructure21 = tower21.pos.findClosestByRange(FIND_STRUCTURES, {
                filter: (structure) => (structure.hits < structure.hitsMax*0.5 && 
                    structure.structureType != STRUCTURE_WALL && structure.structureType != STRUCTURE_RAMPART|| 
                    structure.hits < 25000 && structure.structureType == STRUCTURE_RAMPART ||
                    structure.hits < 25000 && structure.structureType == STRUCTURE_WALL)});
            if(closestDamagedStructure21) {
                tower21.repair(closestDamagedStructure21);
            }
        }
    }
    
   //E9S59
    var tower3 = Game.getObjectById('62278e4c4cff2f44f130feba');
    if(tower3) {
        var closestHostile3 = tower3.room.find(FIND_HOSTILE_CREEPS);
        if(closestHostile3.length>0) {
            tower3.attack(closestHostile3[0]);
        }
        else{
            var closestDamagedStructure3 = tower3.pos.findClosestByRange(FIND_STRUCTURES, {
                filter: (structure) => (structure.hits < structure.hitsMax*0.5 && 
                    structure.structureType != STRUCTURE_WALL && structure.structureType != STRUCTURE_RAMPART|| 
                    structure.hits < 25000 && structure.structureType == STRUCTURE_RAMPART ||
                    structure.hits < 1000 && structure.structureType == STRUCTURE_WALL)});
            if(closestDamagedStructure3) {
                tower3.repair(closestDamagedStructure3);
            }
        }
    }

    var tower4 = Game.getObjectById('6227df66367b9fb7be011e92');
    if(tower4) {
        var closestHostile4 = tower4.room.find(FIND_HOSTILE_CREEPS);
        if(closestHostile4.length>0) {
            tower4.attack(closestHostile4[closestHostile4.length-1]);
        }
        else{
            var closestDamagedStructure4 = tower4.pos.findClosestByRange(FIND_STRUCTURES, {
                filter: (structure) => (structure.hits < structure.hitsMax*0.5 && 
                    structure.structureType != STRUCTURE_WALL && structure.structureType != STRUCTURE_RAMPART|| 
                    structure.hits < 25000 && structure.structureType == STRUCTURE_RAMPART ||
                    structure.hits < 1000 && structure.structureType == STRUCTURE_WALL)});
            if(closestDamagedStructure4) {
                tower4.repair(closestDamagedStructure4);
            }
        }
    }
    //E2S59
    var tower5 = Game.getObjectById('622a9d8aa2d1f6756dada044');
    if(tower5) {
        var closestHostile5 = tower5.room.find(FIND_HOSTILE_CREEPS);
        if(closestHostile5.length>0) {
            tower5.attack(closestHostile5[0]);
        }
        else{
            var closestDamagedStructure5 = tower5.pos.findClosestByRange(FIND_STRUCTURES, {
                filter: (structure) => (structure.hits < structure.hitsMax*0.5 && 
                    structure.structureType != STRUCTURE_WALL && structure.structureType != STRUCTURE_RAMPART|| 
                    structure.hits < 25000 && structure.structureType == STRUCTURE_RAMPART ||
                    structure.hits < 25000 && structure.structureType == STRUCTURE_WALL)});
            if(closestDamagedStructure5) {
                tower5.repair(closestDamagedStructure5);
            }
        }
    }
    
    if(Game.spawns['Spawn1'].spawning) { 
        var spawningCreep = Game.creeps[Game.spawns['Spawn1'].spawning.name];
        Game.spawns['Spawn1'].room.visual.text(
            '🛠️' + spawningCreep.memory.role,
            Game.spawns['Spawn1'].pos.x + 1, 
            Game.spawns['Spawn1'].pos.y, 
            {align: 'left', opacity: 0.8});
    }

    for(var name in Game.creeps) {
        var creep = Game.creeps[name];
        if(creep.memory.role == 'hbru_i') {
            roleHBRU_I.run(creep);
        }
        if(creep.memory.role == 'e10s58_h_i') {
            roleE10S58_H_I.run(creep);
        }
        if(creep.memory.role == 'e2s59_hbru_i') {
            roleE2S59_HBRU_I.run(creep);
        }
        if(creep.memory.role == 'e2s59_hbru_ii') {
            roleE2S59_HBRU_II.run(creep);
        }
        if(creep.memory.role == 'e8s57_hbr_i') {
            roleE8S57_HBR_I.run(creep);
        }
        if(creep.memory.role == 'e8s57_r_i') {
            roleE8S57_R_I.run(creep);
        }
        if(creep.memory.role == 'e8s57_hbr_ii') {
            roleE8S57_HBR_II.run(creep);
        }
        if(creep.memory.role == 'e8s57_fighter_ah') {
            roleE8S57_fighter_AH.run(creep);
        }
        if(creep.memory.role == 'e9s57_hbr_i') {
            roleE9S57_HBR_I.run(creep);
        }
        if(creep.memory.role == 'e9s57_hbr_ii') {
            roleE9S57_HBR_II.run(creep);
        }
        if(creep.memory.role == 'e9s57_r_i') {
            roleE9S57_R_I.run(creep);
        }
        if(creep.memory.role == 'e9s57_fighter_ah') {
            roleE9S57_fighter_AH.run(creep);
        }
        if(creep.memory.role == 'har') {
            roleHar.run(creep);
        }
        if(creep.memory.role == 'har_o') {
            roleHar_o.run(creep);
        }
        if(creep.memory.role == 'e9s59_hc') {
            roleE9S59_HC.run(creep);
        }
        if(creep.memory.role == 'e9s59_t_i') {
            roleE9S59_T_I.run(creep);
        }
        if(creep.memory.role == 'e9s59_u') {
            roleE9S59_U.run(creep);
        }
        if(creep.memory.role == 'stander') {
            roleStander.run(creep);
        }
        if(creep.memory.role == 'stander_e8s57') {
             roleStander_E8S57.run(creep);
        }
        if(creep.memory.role == 'fighter') {
            roleFighter.run(creep);
        }
        if(creep.memory.role == 'fighter_ah') {
            roleFighter_AH.run(creep);
        }
        if(creep.memory.role == 'carrierdrop') {
            roleCarrierdrop.run(creep);
        }
        if(creep.memory.role == 'upgrader') {
            roleUpgrader.run(creep);
        }
        if(creep.memory.role == 'repairer') {
            roleRepairer.run(creep);
        }
        if(creep.memory.role == 'builder') {
            roleBuilder.run(creep);
        }
        if(creep.memory.role == 'builderout') {
            roleBuilderout.run(creep);
        }
        if(creep.memory.role == 'transporter') {
            roleTransporter.run(creep);
        }
        if(creep.memory.role == 'transportertotower') {
            roleTransportertotower.run(creep);
        }
        if(creep.memory.role == 'hario') {
            roleHario.run(creep);
        }
        if(creep.memory.role == 'carrierio') {
            roleCarrierio.run(creep);
        }
        if(creep.memory.role == 'carrierlab') {
            roleCarrierlab.run(creep);
        }
        if(creep.memory.role == 'mover') {
            rolemover.run(creep);
        }
    }

    //数量检测和出生功能
    if(Game.time % 11 == 0){
        //在E9S59
        var hbru_i = _.filter(Game.creeps, (creep) => creep.memory.role == 'hbru_i');
        console.log('hbru_i: ' + hbru_i.length);
        if(hbru_i.length<0) {
            var newName = 'hbru_i' + Game.time;
            console.log('Spawning new hbru_i: ' + newName);
        Game.spawns['Spawn2'].spawnCreep([WORK,CARRY,MOVE,WORK,CARRY,MOVE,WORK,CARRY,MOVE,WORK,CARRY,MOVE,WORK,CARRY,MOVE,WORK,CARRY,MOVE], newName,{memory: {role: 'hbru_i'}}); 
        }

        //采集生物
        var e10s58_h_i = _.filter(Game.creeps, (creep) => creep.memory.role == 'e10s58_h_i');
        console.log('e10s58_h_i: ' + e10s58_h_i.length);
        if(e10s58_h_i.length < 0) {
            var newName = 'e10s58_h_i' + Game.time;
            console.log('Spawning new e10s58_h_i: ' + newName);
            Game.spawns['Spawn1'].spawnCreep([WORK,WORK,WORK,WORK,WORK,
            CARRY,CARRY,
            MOVE,MOVE,MOVE,MOVE,MOVE,MOVE,MOVE], newName,
                {memory: {role: 'e10s58_h_i'}});
        }

        var e2s59_hbru_i = _.filter(Game.creeps, (creep) => creep.memory.role == 'e2s59_hbru_i');
        console.log('e2s59_hbru_i: ' +e2s59_hbru_i.length);
        if(e2s59_hbru_i.length < 4) {
            var newName = 'e2s59_hbru_i' + Game.time;
            console.log('Spawning new e2s59_hbru_i: ' + newName);
            Game.spawns['Spawn3'].spawnCreep([WORK,WORK,WORK,WORK,WORK,
            CARRY,CARRY,CARRY,CARRY,CARRY,
            MOVE,MOVE,MOVE,MOVE,MOVE], newName,
                {memory: {role: 'e2s59_hbru_i'}});
        }
        
        var e2s59_hbru_ii = _.filter(Game.creeps, (creep) => creep.memory.role == 'e2s59_hbru_ii');
        console.log('e2s59_hbru_ii: ' +e2s59_hbru_ii.length);
        if(e2s59_hbru_ii.length < 4) {
            var newName = 'e2s59_hbru_ii' + Game.time;
            console.log('Spawning new e2s59_hbru_ii: ' + newName);
            Game.spawns['Spawn3'].spawnCreep([WORK,WORK,WORK,WORK,WORK,
            CARRY,CARRY,CARRY,CARRY,CARRY,
            MOVE,MOVE,MOVE,MOVE,MOVE], newName,
                {memory: {role: 'e2s59_hbru_ii'}});
        }

        var e8s57_r_i = _.filter(Game.creeps, (creep) => creep.memory.role == 'e8s57_r_i');
        console.log('e8s57_r_i: ' + e8s57_r_i.length);
        if(e8s57_r_i.length < 0) {
            var newName = 'e8s57_r_i' + Game.time;
            console.log('Spawning new e8s57_r_i: ' + newName);
            Game.spawns['Spawn1'].spawnCreep([WORK,WORK,WORK,WORK,
            CARRY,CARRY,CARRY,CARRY,
            MOVE,MOVE,MOVE,MOVE], newName,
                {memory: {role: 'e8s57_r_i'}});
        }
        
        var e8s57_hbr_i = _.filter(Game.creeps, (creep) => creep.memory.role == 'e8s57_hbr_i');
        console.log('e8s57_hbr_i: ' + e8s57_hbr_i.length);
        if(e8s57_hbr_i.length < 0) {
            var newName = 'e8s57_hbr_i' + Game.time;
            console.log('Spawning new e8s57_hbr_i: ' + newName);
            Game.spawns['Spawn1'].spawnCreep([WORK,WORK,WORK,
            CARRY,CARRY,
            MOVE,MOVE,MOVE,MOVE,MOVE,MOVE,MOVE,MOVE,MOVE,MOVE], newName,
                {memory: {role: 'e8s57_hbr_i'}});
        }
         
        var e8s57_hbr_ii = _.filter(Game.creeps, (creep) => creep.memory.role == 'e8s57_hbr_ii');
        console.log('e8s57_hbr_ii: ' + e8s57_hbr_ii.length);
        if(e8s57_hbr_ii.length < 0) {
            var newName = 'e8s57_hbr_ii' + Game.time;
            console.log('Spawning new e8s57_hbr_ii: ' + newName);
            Game.spawns['Spawn1'].spawnCreep([WORK,WORK,WORK,WORK,WORK,WORK,WORK,WORK,WORK,WORK,WORK,
            CARRY,CARRY,CARRY,CARRY,CARRY,CARRY,CARRY,CARRY,CARRY,CARRY,CARRY,
            MOVE,MOVE,MOVE,MOVE,MOVE,MOVE,MOVE,MOVE,MOVE,MOVE,MOVE], newName,
                {memory: {role: 'e8s57_hbr_ii'}});
        }
        
        var e8s57_fighter_ah = _.filter(Game.creeps, (creep) => creep.memory.role == 'e8s57_fighter_ah');
        console.log('e8s57_fighter_ah: ' + e8s57_fighter_ah.length);
        if(e8s57_fighter_ah.length < 0) {
            var newName = 'e8s57_fighter_ah' + Game.time;
            console.log('Spawning new e8s57_fighter_ah: ' + newName);
            Game.spawns['Spawn1'].spawnCreep([RANGED_ATTACK,RANGED_ATTACK,RANGED_ATTACK,RANGED_ATTACK,RANGED_ATTACK,RANGED_ATTACK,MOVE,MOVE,MOVE,MOVE,MOVE,MOVE,MOVE,MOVE], newName,
                {memory: {role: 'e8s57_fighter_ah'}});
        }
        
        var stander_e8s57 = _.filter(Game.creeps, (creep) => creep.memory.role == 'stander_e8s57');
        console.log('Stander_e8s57: ' + stander_e8s57.length);
        if(stander_e8s57.length <0) {
            var newName = 'stander_e8s57' + Game.time;
            console.log('Spawning new stander_e8s57: ' + newName);
            Game.spawns['Spawn1'].spawnCreep([CLAIM,CLAIM,MOVE,MOVE], newName,
                {memory: {role: 'stander_e8s57'}});
        }

        var stander = _.filter(Game.creeps, (creep) => creep.memory.role == 'stander');
        console.log('Stander: ' + stander.length);
        if(stander.length < 0) {
            var newName = 'stander' + Game.time;
            console.log('Spawning new stander: ' + newName);
            Game.spawns['Spawn1'].spawnCreep([CLAIM,MOVE,MOVE], newName,
                {memory: {role: 'stander'}});
        } 
        
        var e9s57_hbr_i = _.filter(Game.creeps, (creep) => creep.memory.role == 'e9s57_hbr_i');
        console.log('e9s57_hbr_i: ' + e9s57_hbr_i.length);
        if(e9s57_hbr_i.length < 1) {
            var newName = 'e9s57_hbr_i' + Game.time;
            console.log('Spawning new e9s57_hbr_i: ' + newName);
            Game.spawns['Spawn4'].spawnCreep([WORK,WORK,WORK,WORK,WORK,WORK,WORK,WORK,
            CARRY,CARRY,CARRY,CARRY,CARRY,CARRY,CARRY,CARRY,
            MOVE,MOVE,MOVE,MOVE,MOVE,MOVE,MOVE,MOVE], newName,
                {memory: {role: 'e9s57_hbr_i'}});
        }

        var e9s57_hbr_ii = _.filter(Game.creeps, (creep) => creep.memory.role == 'e9s57_hbr_ii');
        console.log('e9s57_hbr_ii: ' + e9s57_hbr_ii.length);
        if(e9s57_hbr_ii.length < 0) {
            var newName = 'e9s57_hbr_ii' + Game.time;
            console.log('Spawning new e9s57_hbr_ii: ' + newName);
            Game.spawns['Spawn1'].spawnCreep([WORK,WORK,WORK,WORK,WORK,WORK,WORK,WORK,WORK,WORK,WORK,
            CARRY,CARRY,CARRY,CARRY,CARRY,CARRY,CARRY,CARRY,CARRY,CARRY,CARRY,
            MOVE,MOVE,MOVE,MOVE,MOVE,MOVE,MOVE,MOVE,MOVE,MOVE,MOVE], newName,
                {memory: {role: 'e9s57_hbr_ii'}});
        }
        
        var e9s57_r_i = _.filter(Game.creeps, (creep) => creep.memory.role == 'e9s57_r_i');
        console.log('e9s57_r_i: ' + e9s57_r_i.length);
        if(e9s57_r_i.length < 0) {
            var newName = 'e9s57_r_i' + Game.time;
            console.log('Spawning new e9s57_r_i: ' + newName);
            Game.spawns['Spawn1'].spawnCreep([WORK,WORK,WORK,WORK,
            CARRY,CARRY,CARRY,CARRY,
            MOVE,MOVE,MOVE,MOVE], newName,
                {memory: {role: 'e9s57_r_i'}});
        }
              
        var e9s57_fighter_ah = _.filter(Game.creeps, (creep) => creep.memory.role == 'e9s57_fighter_ah');
        console.log('e9s57_fighter_ah: ' + e9s57_fighter_ah.length);
        if(e9s57_fighter_ah.length < 0) {
            var newName = 'e9s57_fighter_ah' + Game.time;
            console.log('Spawning new e9s57_fighter_ah: ' + newName);
            Game.spawns['Spawn1'].spawnCreep([RANGED_ATTACK,RANGED_ATTACK,RANGED_ATTACK,RANGED_ATTACK,RANGED_ATTACK,RANGED_ATTACK,MOVE,MOVE,MOVE,MOVE,MOVE,MOVE,MOVE,MOVE], newName,
                {memory: {role: 'e9s57_fighter_ah'}});
        }

        var e9s59_hc = _.filter(Game.creeps, (creep) => creep.memory.role == 'e9s59_hc');
        console.log('e9s59_hc: ' + e9s59_hc.length);
        if(e9s59_hc.length < 1) {
            var newName = 'e9s59_hc' + Game.time;
            console.log('Spawning new e9s59_hc: ' + newName);
            Game.spawns['Spawn2'].spawnCreep([WORK,WORK,WORK,WORK,WORK,
            CARRY,CARRY,CARRY,
            MOVE,MOVE,MOVE,MOVE], newName,
                {memory: {role: 'e9s59_hc'}});
        }
        
        var e9s59_t_i = _.filter(Game.creeps, (creep) => creep.memory.role == 'e9s59_t_i');
        console.log('e9s59_t_i: ' + e9s59_t_i.length);
        if(e9s59_t_i.length < 1) {
            var newName = 'e9s59_t_i' + Game.time;
            console.log('Spawning new e9s59_t_i: ' + newName);
            Game.spawns['Spawn2'].spawnCreep([WORK,CARRY,MOVE,
                                            CARRY,CARRY,MOVE,
                                            CARRY,CARRY,MOVE], newName,
                {memory: {role: 'e9s59_t_i'}});
        }
        
        var e9s59_u = _.filter(Game.creeps, (creep) => creep.memory.role == 'e9s59_u');
        console.log('e9s59_u: ' + e9s59_u.length);
        if(e9s59_u.length < 1) {
            var newName = 'e9s59_u' + Game.time;
            console.log('Spawning new e9s59_u: ' + newName);
            Game.spawns['Spawn2'].spawnCreep([WORK,WORK,WORK,WORK,WORK,WORK,WORK,WORK,
            CARRY,CARRY,CARRY,CARRY,
            MOVE,MOVE,MOVE,MOVE,MOVE,MOVE], newName,
                {memory: {role: 'e9s59_u'}});
        }
        
        var fighter = _.filter(Game.creeps, (creep) => creep.memory.role == 'fighter');
        console.log('fighter: ' + fighter.length);
        if(fighter.length < 0) {
            var newName = 'fighter' + Game.time;
            console.log('Spawning new fighter: ' + newName);
            Game.spawns['Spawn1'].spawnCreep([ATTACK,ATTACK,ATTACK,ATTACK,ATTACK,ATTACK,ATTACK,MOVE,MOVE,MOVE,MOVE,MOVE,MOVE,MOVE], newName,
                {memory: {role: 'fighter'}});
        }  
  
        var fighter_ah = _.filter(Game.creeps, (creep) => creep.memory.role == 'fighter_ah');
        console.log('fighter_ah: ' + fighter_ah.length);
        if(fighter_ah.length < 0) {
            var newName = 'fighter_ah' + Game.time;
            console.log('Spawning new fighter_ah: ' + newName);
            Game.spawns['Spawn1'].spawnCreep([RANGED_ATTACK,RANGED_ATTACK,RANGED_ATTACK,RANGED_ATTACK,RANGED_ATTACK,RANGED_ATTACK,MOVE,MOVE,MOVE,MOVE,MOVE,MOVE,MOVE,MOVE], newName,
                {memory: {role: 'fighter_ah'}});
        }

        var har = _.filter(Game.creeps, (creep) => creep.memory.role == 'har');
        console.log('Har: ' + har.length);
        if(har.length < 1) { 
            var newName = 'har' + Game.time;
            console.log('Spawning new har: ' + newName);
            Game.spawns['Spawn1'].spawnCreep([WORK,WORK,WORK,WORK,WORK,CARRY,CARRY,CARRY,CARRY,CARRY,MOVE,MOVE,MOVE,MOVE,MOVE], newName,
                {memory: {role: 'har'}});
        }
        
        var har_o = _.filter(Game.creeps, (creep) => creep.memory.role == 'har_o');
        console.log('Har_o: ' + har_o.length);
        if(har_o.length < 1 && Game.getObjectById('5bbcb32740062e4259e94141').mineralAmount != 0) { 
            var newName = 'har_o' + Game.time;
            console.log('Spawning new har_o: ' + newName);
            Game.spawns['Spawn1'].spawnCreep([WORK,WORK,WORK,WORK,WORK,WORK,WORK,
                                            MOVE,MOVE,MOVE,MOVE,MOVE,MOVE,MOVE,
                                            CARRY,CARRY,CARRY,CARRY,CARRY,CARRY,CARRY], newName,
                {memory: {role: 'har_o'}});
        }
        
        var carrierdrop = _.filter(Game.creeps, (creep) => creep.memory.role == 'carrierdrop');
        console.log('carrierdrop: ' + carrierdrop.length);
        if(carrierdrop.length < 1) {
            var newName = 'carrierdrop' + Game.time;
            console.log('Spawning new carrierdrop: ' + newName);
            Game.spawns['Spawn1'].spawnCreep([CARRY,CARRY,CARRY,CARRY,CARRY,CARRY,CARRY,CARRY,CARRY,CARRY,MOVE,MOVE,MOVE,MOVE,MOVE], newName,
                {memory: {role: 'carrierdrop'}});
        }
        
        var upgrader = _.filter(Game.creeps, (creep) => creep.memory.role == 'upgrader');
        console.log('upgrader: ' + upgrader.length);
        if(upgrader.length < 3) {//6
            var newName = 'upgrader' + Game.time;
            console.log('Spawning new upgrader: ' + newName);
            Game.spawns['Spawn1'].spawnCreep([WORK,WORK,WORK,WORK,WORK,WORK,WORK,WORK,WORK,WORK,
            CARRY,CARRY,CARRY,CARRY,CARRY,CARRY,CARRY,CARRY,CARRY,CARRY,
            MOVE,MOVE,MOVE,MOVE,MOVE,MOVE,MOVE,MOVE,MOVE,MOVE], newName,
                {memory: {role: 'upgrader'}});
        }

        var repairer = _.filter(Game.creeps, (creep) => creep.memory.role == 'repairer');
        console.log('repairer: ' + repairer.length);
        if(repairer.length < 0) {
            var newName = 'repairer' + Game.time;
            console.log('Spawning new repairer: ' + newName);
            Game.spawns['Spawn1'].spawnCreep([WORK,WORK,WORK,WORK,WORK,WORK,CARRY,CARRY,CARRY,CARRY,CARRY,CARRY,MOVE,MOVE,MOVE,MOVE,MOVE,MOVE], newName,
                {memory: {role: 'repairer'}});
        }

        var builder = _.filter(Game.creeps, (creep) => creep.memory.role == 'builder');
        console.log('builder: ' + builder.length);
        if(builder.length < 1) {
            var newName = 'builder' + Game.time;
            console.log('Spawning new builder: ' + newName);
            Game.spawns['Spawn1'].spawnCreep([WORK,WORK,WORK,WORK,WORK,CARRY,CARRY,CARRY,CARRY,CARRY,MOVE,MOVE,MOVE,MOVE,MOVE], newName,
                {memory: {role: 'builder'}});
        } 

         var builderout = _.filter(Game.creeps, (creep) => creep.memory.role == 'builderout');
         console.log('builderout: ' + builderout.length);
         if(builderout.length <0) {
             var newName = 'builderout' + Game.time;
             console.log('Spawning new builderout: ' + newName);
             Game.spawns['Spawn1'].spawnCreep([WORK,WORK,CARRY,CARRY,CARRY,CARRY,MOVE,MOVE,MOVE], newName,
                 {memory: {role: 'builderout'}});
         } 

        var transporter = _.filter(Game.creeps, (creep) => creep.memory.role == 'transporter');
        console.log('transporter: ' + transporter.length);
        if(transporter.length < 0) {
            var newName = 'transporter' + Game.time;
            console.log('Spawning new transporter: ' + newName);
            Game.spawns['Spawn1'].spawnCreep([CARRY,CARRY,CARRY,CARRY,CARRY,CARRY,CARRY,CARRY,MOVE,MOVE,MOVE,MOVE,CARRY,CARRY,CARRY,CARRY,CARRY,CARRY,CARRY,CARRY,MOVE,MOVE,MOVE,MOVE], newName,
                {memory: {role: 'transporter'}});
        }

        var transportertotower = _.filter(Game.creeps, (creep) => creep.memory.role == 'transportertotower');
        console.log('transportertotower: ' + transportertotower.length);
        if(transportertotower.length < 1) {
            var newName = 'transportertotower' + Game.time;
            console.log('Spawning new transportertotower: ' + newName);
            Game.spawns['Spawn1'].spawnCreep([CARRY,CARRY,CARRY,CARRY,CARRY,CARRY,CARRY,CARRY,CARRY,CARRY,MOVE,MOVE,MOVE,MOVE,MOVE], newName,
                {memory: {role: 'transportertotower'}});
        }
        
        var hario = _.filter(Game.creeps, (creep) => creep.memory.role == 'hario');
        console.log('Hario: ' + hario.length);
        if(hario.length < 1) {
            var newName = 'hario' + Game.time;
            console.log('Spawning new hario: ' + newName);
            Game.spawns['Spawn1'].spawnCreep([WORK,WORK,WORK,WORK,WORK,CARRY,CARRY,CARRY,CARRY,CARRY,MOVE,MOVE,MOVE,MOVE,MOVE], newName,
                {memory: {role: 'hario'}}); 
        }  
  
        var carrierio = _.filter(Game.creeps, (creep) => creep.memory.role == 'carrierio');
        console.log('carrierio: ' + carrierio.length);
        if(carrierio.length<1){
            var newName = 'carrierio' + Game.time;
            console.log('Spawning new carrierio: ' + newName);
            Game.spawns['Spawn1'].spawnCreep([CARRY,CARRY,MOVE], newName,
                {memory: {role: 'carrierio'}});
        }
        
        var carrierlab = _.filter(Game.creeps, (creep) => creep.memory.role == 'carrierlab');
        console.log('carrierlab : ' + carrierlab.length);
        if(carrierlab.length<1){
            var newName = 'carrierlab' + Game.time;
            console.log('Spawning new carrierlab: ' + newName);
            Game.spawns['Spawn4'].spawnCreep([CARRY,CARRY,MOVE,
            CARRY,CARRY,MOVE], newName,
                {memory: {role: 'carrierlab'}});
        }
        
        var mover = _.filter(Game.creeps, (creep) => creep.memory.role == 'mover');
        console.log('mover: ' + mover.length);
        if(mover.length<0){
            var newName = 'mover' + Game.time;
            console.log('Spawning new mover: ' + newName);
            Game.spawns['Spawn1'].spawnCreep([MOVE], newName,
                {memory: {role: 'mover'}});
        }
    }
}
